# --------------------------------------------------------------------
#
# Plugin:       DateLang
# Description:  Enter date in full according to your location.
# Version: 0.1.0
# Author:       William Canin - https://github.com/williamcanin
#
# Support locales:
# ch_CH - 中国
# de_DE - Deutschland
# en_US - English
# es_ES - Español
# fr_FR - Français
# it_IT - Italiano
# ja_JP - 日本の
# pt_PT - Portuguese
# ru_RU - Pусский


# Usage:

# 1 - Copy this file to folder "_plugins".

# 2 - Add the following to the "_config.yml" file:

# datelang:
#   locale: <ch_CH | de_DE | en_US | es_ES | fr_FR | it_IT | ja_JP | pt_PT | ru_RU>
#   reload: <true|false> - default: false

# 3 - Copy the file "datelang.json" to the folder "src/config/json".

# 4 - In your HTML files, call DateLang according to the variable used in the posts loop. Example:
#
#     {% for post in site.posts %}
#       {% datelang post %}
#       {% include datelang/post %}
#     {% endfor %}

# Note: If you want to change the language, leave "reload: true", and the next time you compile or start the Jekyll server, you will change your location.

#
# --------------------------------------------------------------------

# import libs
require "fileutils"


module Jekyll
  module Datelang
    class Main < Liquid::Tag
      Syntax = /^\s*([^\s]+)(\s+(\d+)\s+(\d+)\s*)?/
      def get_configs
        @config_json = File.join(File.dirname(File.expand_path(__FILE__)), "./.datelangrc")
        if not File.exist?(@config_json)
          raise "Abort: File \"#{@config_json}\" not found. You must create along with its structure."
        end
        @config_json_read = File.read(@config_json)
        @data_hash_json = JSON.parse(@config_json_read)
        @data_hash_yml = YAML::load_file(File.join(File.dirname(File.expand_path(__FILE__)), '../_config.yml'))
        @locale = @data_hash_yml['_plugins']['datelang']['locale']
        @reload = @data_hash_yml['_plugins']['datelang']['reload']
        @path_datelang = File.join("./", "_includes/datelang")
      end # end get_configs
     def tplate(path,fileC,data_hash_json,locale, reload)
        @path = path
        @fileC = fileC
        @data_hash_json = data_hash_json
        @locale = locale
        @reload = reload

          if not File.exists?("#{@path}/#{@fileC}") or @reload == true
            File.open("#{@path}/#{@fileC}", "w") do |f_create|
                f_create.puts "
{% comment %}
This file was automatically generated by the \"DateLang\" plugin.
Do not change this file. It will be generated every time it is not present.
Thank you. :) att. William Canin
{% endcomment %}

{% assign m = #{@fileC}.date | date: \"%-m\" %}
{% case m %}
  {% when '1' %}#{@data_hash_json[@locale]['DateLang']['month1']}
  {% when '2' %}#{@data_hash_json[@locale]['DateLang']['month2']}
  {% when '3' %}#{@data_hash_json[@locale]['DateLang']['month3']}
  {% when '4' %}#{@data_hash_json[@locale]['DateLang']['month4']}
  {% when '5' %}#{@data_hash_json[@locale]['DateLang']['month5']}
  {% when '6' %}#{@data_hash_json[@locale]['DateLang']['month6']}
  {% when '7' %}#{@data_hash_json[@locale]['DateLang']['month7']}
  {% when '8' %}#{@data_hash_json[@locale]['DateLang']['month8']}
  {% when '9' %}#{@data_hash_json[@locale]['DateLang']['month9']}
  {% when '10' %}#{@data_hash_json[@locale]['DateLang']['month10']}
  {% when '11' %}#{@data_hash_json[@locale]['DateLang']['month11']}
  {% when '12' %}#{@data_hash_json[@locale]['DateLang']['month12']}
{% endcase %}
{{ #{@fileC}.date | date: \"%d,\" }}
{{ #{@fileC}.date | date: \"%-Y\" }}"
            end
          end

      end # end tplate
      def initialize(tagName, markup, tokens)
        super
        if markup =~ Syntax then
          @tpe = $1
        else
          raise "Warning! No parameter was inserted in the plugin!"
        end
        get_configs
      end # initialize
      def render(context)
        get_configs
        unless File.directory?(@path_datelang)
          FileUtils.mkdir_p(@path_datelang)
        end
        tplate(@path_datelang, @tpe, @data_hash_json, @locale, @reload)
      end # end render
    end # end Main

  end
end

# Register tag
Liquid::Template.register_tag('datelang', Jekyll::Datelang::Main)
